package com.hoki.zj.hrm.controller;

import com.hoki.zj.hrm.doc.CourseDoc;
import com.hoki.zj.hrm.interceptor.utils.ESSearchBucketsList;
import com.hoki.zj.hrm.query.CourseSearchQuery;
import com.hoki.zj.hrm.service.ICourseESService;
import com.hoki.zj.hrm.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/es/course")
public class CourseESController {

    @Autowired
    private ICourseESService courseESService;

    /**
     * 1.ElasticSearch添加/修改
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('es:saveCourse')")
    public AjaxResult save(@RequestBody CourseDoc courseDoc) {
        courseESService.save(courseDoc);
        return AjaxResult.me();
    }

    /**
     * 2.ElasticSearch删除(コースサービス終了)
     */
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        courseESService.del(id);
        return AjaxResult.me();
    }

    /**
     * 3.ElasticSearch 前台页面列表展示(分页/高级)
     * @return AjaxResult
     */
    @PostMapping("search")
    public AjaxResult search(@RequestBody CourseSearchQuery courseSearchQuery) {
        ESSearchBucketsList courseDocs = courseESService.search(courseSearchQuery);
        return new AjaxResult().setResultObj(courseDocs);
    }
}
